//
//  WomenChatViewRootViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/3/26.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "WomenChatViewRootViewController.h"
#import <TXLiteAVSDK_Professional/TXLivePlayer.h>
#import "RtmpManage.h"
#import "VideoChatUserView.h"
#import "UserInfoViewController.h"
#import "CallUserManger.h"
#import "CallUserViewController.h" //呼叫页面
#import "UserInfoLogic.h"
#import "VideoChatUserViewSmall.h"
@interface WomenChatViewRootViewController ()<RtmpManageDelegate,UserInfoLogicDelegate>
@property(nonatomic, strong) VideoChatUserViewSmall *Video1;
@property(nonatomic, strong) VideoChatUserViewSmall *Video2;
@property(nonatomic, strong) VideoChatUserViewSmall *Video3;
@property(nonatomic, strong) CallUserManger *callManger;
@property(nonatomic, strong)  UserInfoLogic *logic;

@property(nonatomic, strong) QMUIFillButton *btnGetNew;
@property(nonatomic, strong) RtmpManage *rtmpManage;

@property(nonatomic, strong) NSMutableArray *listview;
@property(nonatomic, assign) int status;
@property(nonatomic, strong) UILabel *tip;
@property(nonatomic, strong) UILabel *tip2;
@property(nonatomic, strong) UILabel *tip3;

@end


@implementation WomenChatViewRootViewController
{
    int _status;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = ASLocalizedString(@"视频聊");
    
    
    _logic = [UserInfoLogic new];
    _logic.delegagte = self;
    
    _Video1 = [[VideoChatUserViewSmall alloc] initWithFrame:CGRectMake(2, 2, 2, 2)];
    _Video2 = [[VideoChatUserViewSmall alloc] initWithFrame:CGRectMake(2, 2, 2, 2)];
    _Video3 = [[VideoChatUserViewSmall alloc] initWithFrame:CGRectMake(2, 2, 2, 2)];
    
    _tip = [[UILabel alloc] init];
    _tip.text = ASLocalizedString(@"当前用户(0人在线)");
    _tip.textColor = KWhiteColor;
    
    _tip2 = [[UILabel alloc] init];
    _tip2.text = ASLocalizedString(@"图像直播中，等待来电..");
    _tip2.textColor = KWhiteColor;
    _tip2.font = [UIFont systemFontOfSize:14];
    
    _tip3 = [[UILabel alloc] init];
    _tip3.text = ASLocalizedString(@"一键约爱随机来电，接通只获系统最低奖励");
    _tip3.textColor = MainColor2;
    _tip3.font = [UIFont systemFontOfSize:15];
    
    _callManger = [[CallUserManger alloc] init];
    @weakify(self)
    _Video1.UserVideoChatClick = ^(RandList *model) {
        [weak_self callWithUID:StringFromInt(model.id)];
        //        [weak_self.callManger CallUserWithUID:StringFromInt(model.id) AndVC:weak_self];
        //        UserInfoViewController *user = [UserInfoViewController new];
        //        user.uid = StringFromInt(model.id);
        //        [weak_self.navigationController pushViewController:user animated:NO];
    };
    
    _Video2.UserVideoChatClick = ^(RandList *model) {
        [weak_self callWithUID:StringFromInt(model.id)];
        //        UserInfoViewController *user = [UserInfoViewController new];
        //        user.uid = StringFromInt(model.id);
        //        [weak_self.navigationController pushViewController:user animated:NO];
    };
    
    _Video3.UserVideoChatClick = ^(RandList *model) {
        [weak_self callWithUID:StringFromInt(model.id)];
        //        UserInfoViewController *user = [UserInfoViewController new];
        //        user.uid = StringFromInt(model.id);
        //        [weak_self.navigationController pushViewController:user animated:NO];
    };
    
    
    _Video1.backgroundColor = [UIColor whiteColor];
    _Video2.backgroundColor = [UIColor whiteColor];
    _Video3.backgroundColor = [UIColor whiteColor];
    
    _listview = [NSMutableArray array];
    [_listview addObject:_Video1];
    [_listview addObject:_Video2];
    [_listview addObject:_Video3];
    
    [self.view addSubview:_Video1];
    [self.view addSubview:_Video2];
    [self.view addSubview:_Video3];
    
    [self.view addSubview:_tip];
    [self.view addSubview:_tip2];
    [self.view addSubview:_tip3];
    
    _btnGetNew = [[QMUIFillButton alloc] init];
    _btnGetNew.fillColor = MainColor;
    [_btnGetNew setTitle:ASLocalizedString(@"换一批")forState:UIControlStateNormal];
    [_btnGetNew addTarget:self action:@selector(getvideo) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_btnGetNew];
    [self setLayout];
    
    _preViewContainer.backgroundColor = [UIColor lightGrayColor];
    //    [self.view sendSubviewToBack:_preViewContainer];
    _rtmpManage = [RtmpManage new];
    _rtmpManage.delegate = self;
}


//测试
- (void)callWithUID:(NSString *)uid {
    
    [[HUDHelper sharedInstance] syncLoading];
    
    _logic.uid = uid;
    //防止请求用户失败一直不消失菊花
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if(_status == 0)
        {
            [[HUDHelper sharedInstance] syncStopLoading];
        }
    });
    [_logic loadDataNoCache];
    
}

#pragma mark - 数据加载完成回调
-(void)requestDataCompleted
{
    kWeakSelf(self)
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"video_call_api" A:@"video_call"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:_logic.uid forKey:@"id"];
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
        
    } success:^(id responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
            _status = 1;
            [[HUDHelper sharedInstance] syncStopLoading];
            //跳转到视频界面
            CallUserViewController *callView = [CallUserViewController new];
            callView.usermodel = _logic.model;
            callView.channel = [responseObject valueForKey:@"channel_id"];
            callView.iscall = YES;
            [weakself.navigationController pushViewController:callView animated:NO];
            //            [self.navigationController pushViewController:callView animated:YES];
        });
        
    } failure:^(NSString *error) {
        [[HUDHelper sharedInstance] syncStopLoading];
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
    
}

-(void)getvideo
{
    //    [[HUDHelper sharedInstance] syncLoading:ASLocalizedString(@"获取中")inView:self.view];
    [_rtmpManage getonlineUser];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    [_rtmpManage update_live];
    [_rtmpManage getonlineUser];
    
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //释放推流
    [self stopRtmp];
    
    //释放播放
    //    for (int i=0; i<_listview.count; i++) {
    //        VideoChatUserView *videoView = _listview[i];
    //        [videoView stopPlay];
    //    }
    [_rtmpManage removetime];
}

-(void)setLayout
{
    [@[_Video1,_Video2,_Video3] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).offset(-30);
        //        make.width.equalTo(@(SCREEN_WIDTH/2));
        make.height.equalTo(@(SCREEN_WIDTH/3*4/3));
    }];
    
    [@[_Video1,_Video2,_Video3] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:3 leadSpacing:0 tailSpacing:0];
    
    [_btnGetNew mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_Video1.mas_bottom).offset(20);
        make.height.equalTo(@40);
        make.width.equalTo(@100);
        make.top.equalTo(_Video1.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
    
    [_tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_Video1.mas_top).offset(-5);
        make.left.equalTo(_Video1);
    }];
    
    
    [_tip2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_btnGetNew.mas_bottom).offset(30);
        make.centerX.equalTo(self.view);
    }];
    
    [_tip3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tip2.mas_bottom).offset(10);
        make.centerX.equalTo(self.view);
    }];
    
    [_preViewContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        //        make.width.equalTo(@(SCREEN_WIDTH/3));
        //        make.height.equalTo(@(SCREEN_WIDTH/3*4/3));
        //        make.right.equalTo(self.view);
        //        make.top.equalTo(self.view);
    }];
}



//rtmp数据更新时
-(void)updateRtmp:(updateLiveModel *)model
{
    self.pushUrlStr = model.push_url;
    NSLog(ASLocalizedString(@"推流地址%@"),model.push_url);
    [self startRtmp];
}

-(void)updateRandVideo:(RandVideoModel *)model
{
    _tip.text =[NSString stringWithFormat: ASLocalizedString(@"当前用户(%@人在线)"),model.online_count];
    
    [[HUDHelper sharedInstance] syncStopLoading];
    for (int i=0; i<model.list.count && i<3; i++) {
        RandList *data = model.list[i];
        VideoChatUserViewSmall *videoView = _listview[i];
        videoView.hidden = NO;
        [videoView setRandVideo:data];
    }
    
    
    
    if(model.list.count == 0)
    {
        
        [MBProgressHUD showTopTipMessage:ASLocalizedString(@"没有人可拨打的用户哦~")];
        VideoChatUserViewSmall *data0 = _listview[0];
        VideoChatUserViewSmall *data1 = _listview[1];
        VideoChatUserViewSmall *data2 = _listview[2];
        data0.hidden = YES;
        data1.hidden = YES;
        data2.hidden = YES;
    }
    
    if(model.list.count == 1)
    {
        VideoChatUserViewSmall *data1 = _listview[1];
        VideoChatUserViewSmall *data2 = _listview[2];
        data1.hidden = YES;
        data2.hidden = YES;
    }
    
    if(model.list.count == 2)
    {
        VideoChatUserViewSmall *data2 = _listview[2];
        data2.hidden = YES;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

